package PartA;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.InputStream;
import java.net.ServerSocket;
import java.net.Socket;


public class EchoServer{
	
	public static void main(String args [])throws Exception{
		
		byte[] buffer = new byte[1024];
		int bytes = 0;
		
		//Set up the sockets and the data streams
		ServerSocket welcomeSocket = new ServerSocket(6789);
		Socket connectionSocket = welcomeSocket.accept();
		InputStream is = connectionSocket.getInputStream();
		DataInputStream dis = new DataInputStream(is);
		DataOutputStream dos = new DataOutputStream(connectionSocket.getOutputStream());
		
		// Loop until the input stream reads -1 for connection closed
		while ((bytes = dis.read(buffer)) != -1) {
			// echo the data to the output stream 
		    dos.write(buffer, 0, bytes);
		}
		
		// close the sockets
		connectionSocket.close();
		welcomeSocket.close();
	}
}
